﻿#region Copyright 2006 - Evolution Tecnologia Ltda.
//
// Author: Rafael Franco Carvalho (rafael@Evolutiontec.com.br)
//
// All rights are reserved. Reproduction or transmission in whole or in part, in
// any form or by any means, eletronic, mechanical or otherwise, is prohibited
// without the prior written permission of the author.
//
// Filename: LoginForm.cs
#endregion

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using ComponentFactory.Krypton.Toolkit;
using csharp.common;

namespace csharp.rad.security
{
    public partial class Login : Form
    {
        const String EvolutionUrl = "http://www.rad.com.br";
        private User myUser;
        private Int32 tries = 0;

        public Login(ref User appUser, String appTitle, String appDescription, String appVersion)
        {
            InitializeComponent();
            myUser = appUser;
            //Fill the labels
            applicationTitleLabel.Text = appTitle;
            applicationDescriptionLabel.Text = appDescription;
            applicationVersionLabel.Text = appVersion;
        }

        private void loginForm_Load(object sender, EventArgs e)
        {
            String logoImage;
            //Set background image
            logoImage = System.IO.Path.GetDirectoryName(Application.ExecutablePath.ToString())
                      + "\\splash.jpg";
            if (System.IO.File.Exists(logoImage))
            {
                this.BackgroundImage = new Bitmap(logoImage);
            }
                       
            //Clean texBoxes
            userNameTextBox.Clear();
            passwordTextBox.Clear();
            userNameTextBox.Focus();
        }

        private void userNameTextBox_Leave(object sender, EventArgs e)
        {
            if (userNameTextBox.Text != "")
            {
                myUser.Clear();
                if (myUser.ValidateUser(userNameTextBox.Text))
                {
                    passwordTextBox.Focus();
                }
                else
                {
                    KryptonMessageBox.Show("Usuário '" + userNameTextBox.Text + "' inexistente."
                                  , "Entrada de sistema", MessageBoxButtons.OK
                                  , MessageBoxIcon.Warning);
                    userNameTextBox.SelectAll();
                    userNameTextBox.Focus();
                }
            }
        }

        private void validatePassword()
        {
            if (passwordTextBox.Text != "")
            {
                if (myUser.ValidatePassword(passwordTextBox.Text))
                {
                    if (myUser.CheckPermission("SISTEMA.ACESSO", User.doCheckPermissionWarning))
                    {
                        this.DialogResult = DialogResult.OK;
                    }
                    else
                    {
                        userNameTextBox.Clear();
                        passwordTextBox.Clear();
                        userNameTextBox.Focus();
                    }
                }
                else
                {
                    KryptonMessageBox.Show("A senha não confere.", "Entrada de sistema"
                                  , MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    tries = tries + 1;
                    if (tries == 3)
                    {
                        KryptonMessageBox.Show("O número máximo de tentativas foi atingido."
                                      , "Entrada de sistema", MessageBoxButtons.OK
                                      , MessageBoxIcon.Warning);
                        this.DialogResult = DialogResult.Cancel;
                    }
                    passwordTextBox.Clear();
                    passwordTextBox.Focus();
                }
            }
        }

        private void passwordTextBox_Leave(object sender, EventArgs e)
        {
            validatePassword();
        }

        private void loginForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Escape))
            {
                Close();
            }
        }

        private void userNameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
            {
                passwordTextBox.Focus();
            }
        }

        private void passwordTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode.Equals(Keys.Enter) || e.KeyCode.Equals(Keys.Return))
            {
                validatePassword();
            }
        }

        private void contatoLabel_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start(EvolutionUrl);
        }

    }
}
